<?php
class mod_repoimages {

private $System	=null;

private $table	="mod_repoimages";

private $smallX = 100;

private $smallY = 0;

private $largeX = 500;

private $largeY = 0;

private $lang	= null;

function __construct ( init & $Sys ){

$this->System=&$Sys;

$this->System->lang->setModuleLanguage("repoimages",array("en","ru","ua"),"en",$this->lang);
}

public function manage(){

/*
 * Adding
 */

if($this->System->checkPOST("Submit","width","height") && isset($_FILES["image"]) && !empty($_FILES["image"]["tmp_name"])){

$Main=$this->System->file->copyFile("image",array("jpg","jpeg","gif","png"));

if(!$Main){

print $this->System->errors->Error($this->System->file->lastError);
    
}
else {
$Small = $this->System->file->cp($Main);

if(!$Small){

print $this->System->errors->Error($this->System->file->lastError);

} else {



$this->System->file->resizeImage($Small,$this->smallX,$this->smallY);

list($width,$height)=$this->System->escapePOST("width","height");

$width=(is_numeric($width) && (int)$width!=0)?$width:$this->largeX;

$height=(is_numeric($height) && (int)$height!=0)?$height:null;

if(!isset($_POST["nochange"])){
$this->System->file->resizeImage($Main,$width,$height);
}
$this->System->dbo->insert($this->table,array("small"=>$Small,"large"=>$Main,"created"=>date("Y-m-d H:i:s") ) );

print $this->System->errors->Message($this->lang->get("r5"));

}
}


}

/*
 * Deleting
 */ 
if(isset($_GET["remove"]) && is_numeric($_GET["remove"])){

$toRemove=$this->System->escapeString($_GET["remove"]);

$Query = $this->System->dbo->select($this->table,"small,large","where image_id='".$toRemove."' limit 1 offset 0",_RESULT_ROW);

if(count($Query)>0){

$this->System->file->delete($Query["small"]);

$this->System->file->delete($Query["large"]);

$this->System->dbo->delete($this->table,"where image_id='".$toRemove."'");
}

print $this->System->errors->Message($this->lang->get("r4") );
}


/*
 * Managing
 */

$Query = $this->System->dbo->select($this->table,"small,large,image_id,created"," order by created desc limit 100 offset 0");

$data=array();
$i=0;while($i<count($Query)){

$data[$Query[$i]["image_id"]]=array("small"=>$this->System->file->GetLink($Query[$i]["small"]),

"large"=>$this->System->file->GetLink($Query[$i]["large"]) );

$i++;
}

return $this->System->view->GetTemplate("repoimages","manage",array("data"=>&$Query,"Image"=>&$data,"count"=>count($Query) ) );

}

private function RemoveAll(){

$Query = $this->System->dbo->select($this->table,"small,large,image_id");

$i=0; while($i<count($Query)){


$this->System->file->delete($Query[$i]["small"]);

$this->System->file->delete($Query[$i]["large"]);

$this->System->dbo->delete($this->table,"where image_id='".$Query[$i]["id"]."'");

$i++;
}

return true;
}




public function install(){

$table["name"]=$this->table;
$table["field"][]=array("name"=>"image_id","type"=>"int_auto");
$table["field"][]=array("name"=>"small","type"=>"int");
$table["field"][]=array("name"=>"large","type"=>"int");
$table["field"][]=array("name"=>"created","type"=>"datetime");
$table["pkey"][]=array("field"=>"image_id");
$this->System->dbo->createtable($table);

}

public function uninstall(){
$this->RemoveAll();
$this->System->dbo->direct("drop table if exists ".$this->table);
}


}
?>